package com.goktech.olala.client.controller.customer;

import com.goktech.olala.core.config.SysConfig;
import com.goktech.olala.core.service.ICtmInfoService;
import com.goktech.olala.server.pojo.customer.CustomerInfo;
import com.goktech.olala.server.pojo.customer.UserLogin;
import com.goktech.olala.server.pojo.customer.UserLoginLog;
import com.goktech.olala.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author wjs
 * @Classname UserController
 * @Description TODO
 * @Date 2020/12/25 23:30
 * @Created by wjs
 */
@Controller
@RequestMapping("/cntApi")
public class AccountController {
    @Autowired
    ICtmInfoService iCtmInfoService;


    @RequestMapping("/login.do")
    @ResponseBody
    public String UserLogin(String userName, String password, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //到数据库查询信息
        UserLogin user = iCtmInfoService.selectUser(userName, password);
        //如果无此人，登录失败
        String msg = "";
        if (user == null) {
            msg = SysConfig.ERROR_CODE_STR;
        } else {
            //成功的话
            msg = SysConfig.SUCCESS_CODE_STR;
            //成功返回用户数据
            request.getSession().setAttribute("customerId", user.getCustomerId());
            request.getSession().setAttribute("loginName", user.getLoginName());

            //用户状态更新
            user.setUserStatus(1);
            iCtmInfoService.updateUserLogin(user);

            //日志更新
            UserLoginLog userLoginLog = new UserLoginLog();
            userLoginLog.setCustomerId(user.getCustomerId());
            userLoginLog.setLoginIp(request.getRemoteAddr());
            userLoginLog.setLoginTime(new Date(System.currentTimeMillis()));
            userLoginLog.setLoginType(1);
            iCtmInfoService.addLoginLog(userLoginLog);
        }
        return msg;
    }

    @PostMapping("/logout.do")
    @ResponseBody
    public String Logout(String userName, String customerId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String msg = "";
        //更新数据库内部用户表用户的状态
        UserLogin user = new UserLogin();
        user.setLoginName(userName);
        user.setCustomerId(customerId);
        user.setUserStatus(0);
        iCtmInfoService.updateUserLogin(user);
        //session移除用户
        request.getSession().removeAttribute("loginName");
        request.getSession().removeAttribute("customerId");
        //数据库用户登录日志表更新用户状态
        UserLoginLog userLoginLog = new UserLoginLog();
        userLoginLog.setCustomerId(user.getCustomerId());
        userLoginLog.setLoginIp(request.getRemoteAddr());
        userLoginLog.setLoginTime(new Date(System.currentTimeMillis()));
        userLoginLog.setLoginType(0);
        iCtmInfoService.addLoginLog(userLoginLog);
        msg = SysConfig.SUCCESS_CODE_STR;
        return msg;
    }

    @GetMapping("/info.do")
    @ResponseBody
    public ModelAndView Info() {
        ModelAndView modelAndView = new ModelAndView();

        String msg = "";

        List<CustomerInfo> customerInfos = iCtmInfoService.selectCustomerInfo();
        modelAndView.addObject("Consignee", customerInfos);

        msg = SysConfig.SUCCESS_CODE_STR;
        modelAndView.addObject("msg", msg);

        modelAndView.setViewName("home/introduction");
        return modelAndView;
    }

    @PostMapping("/register.do")
    @ResponseBody
    public String Register(String email, String password, HttpServletRequest request, HttpServletResponse response) {
        String msg = "";
        //查询是否存在该用户名
        UserLogin userLogin1 = iCtmInfoService.registerSelect(email);
        //如果存在用户
        if (userLogin1 != null) {
            msg = SysConfig.DEFAULT_IDENTITY_CARD_TYPE;
        } else {
            UserLogin userLogin = new UserLogin();
            userLogin.setLoginName(email);
            userLogin.setPassword(MD5Util.md5(password));
            userLogin.setUserStatus(0);
            userLogin.setCustomerId(StringUtils.isBlank(email)?email:email);
            userLogin.setModifiedTime(new Date(System.currentTimeMillis()));
            iCtmInfoService.register(userLogin);
            msg = SysConfig.SUCCESS_CODE_STR;
        }
        return msg;
    }
}
